// 显示欢迎页，进行操作

package ATM;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATM
{
    private ArrayList<Account> list = new ArrayList<Account>();
//    private ArrayList<Account> list1 = new ArrayList<>();
//    private ArrayList<Account> list2 = new ArrayList();
    Scanner sc = new Scanner(System.in);
    int sz = 0;
    int idex = -1;
    Account account = null;

    public ATM()
    {
        int option;
        while (/*1*/true)
        {
            idex = -1;
            System.out.println("========欢迎进入ATM系统========");
            System.out.println("1、用户登录\n2、用户开户");
            System.out.print("请输入您要进行的业务:");

            option = sc.nextInt();
            switch(option) {
                case 1:
                    LoginAccount();
                    break;
                case 2:
                    NewAccount();
                    break;
                default:
                    System.out.println("没有这一个选项，请重新选择");
                    break;
            }
            System.out.println();
        }
    }

    private boolean NewAccount()
    {
        System.out.println("=======用户开户=======");

        Scanner sc = new Scanner(System.in);
        Account account = new Account();
        System.out.println("请输入你要开户的用户名");
        account.setName(sc.next());

        String card = GetCard();
        for (int i = 0; i < sz; i++)
        {
            if (card.equals(list.get(i).getCard()))
            {
                i = 0;
                card = GetCard();
            }
        }
        account.setCard(card);
        System.out.println("您的卡号是：" + card);

        String password1 = "", password2 = "";
        System.out.println("请输入你要设置的密码");
        password1 = sc.next();
        System.out.println("请重复您的密码");
        password2 = sc.next();

        while (!password1.equals(password2)) {
            System.out.println("您的两次密码不一致，请重新输入");
            System.out.println("请输入你要设置的密码");
            password1 = sc.next();
            System.out.println("请重复您的密码");
            password2 = sc.next();
        }
        account.setPassword(password1);

        System.out.print("请设置您的取现额度：");
        account.setMaxx(sc.nextDouble());

        account.setBalance(0.0);

        System.out.println("恭喜您开户成功！");
        list.add(account);
        sz++;
        System.out.println();
        return true;
    }

    private String GetCard()
    {
        Random r = new Random();

//        int[] a = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
//        String s = "0123456789";

        String card = "";
        for (int i = 0; i < 10;  i++)
        {
//            card += s[r.nextInt(10)];// 不可以这样用
//            card += s.charAt(r.nextInt(10));
            card += r.nextInt(10);
        }

        return card;
    }

    private boolean LoginAccount()
    {
        System.out.println("=====用户登录=====");

        if (sz == 0)
        {
            System.out.println("当前无账户");
            System.out.println();
            return false;
        }

        int idex = -1;
        System.out.println("请输入您要登录的卡号:");
        String card = sc.next();
        for (int i = 0; i < sz; i++) {
            if (card.equals(list.get(i).getCard())) {
                idex = i;
                break;
            }
        }
        if (idex == -1) {
            System.out.println("未找到相关卡号，登录失败!");
            System.out.println();
            return false;
        }

        String password;
        for (int i = 0 ;i < 3; i++) {
            System.out.print("请输入您的密码：");
            password = sc.next();

            if (password.equals(list.get(idex).getPassword()))
            {
                System.out.println("密码正确，欢迎您进入账户系统！");
                account = list.get(idex);
                break;
            }
            else
            {
                if (i == 2)
                {
                    System.out.println("密码错误三次以上，登录失败");
                    System.out.println();
                    return false;
                }
                System.out.print("密码错误，您还有");
                System.out.print(3 - 1 - i);
                System.out.println("次机会，请重新输入");
            }
        }

        account.ShowAccount();
        while (true) {
            System.out.println("=======用户操作=======");
            System.out.println("1、存钱");
            System.out.println("2、取钱");
            System.out.println("3、转账");
            System.out.println("4、销户");
            System.out.println("5、修改密码");
            System.out.println("6、退出");
            System.out.print("请输入您要进行的操作：");
            int option;
            option = sc.nextInt();
            switch(option)
            {
                case 1:
                    System.out.print("请输入您要存入的数额：");
                    account.AddMoney(sc.nextDouble());
                    break;
                case 2:
                    System.out.println("请输入您要取出的数额：");
                    account.SubMoney(sc.nextDouble());
                    break;
                case 3:
                    account.TransferMoney(list);
                    break;
                case 4:
                    DeleteAccount();
                case 5:
                    account.ChangePassword();
                case 6:
                    System.out.println("退出账户成功！");
                    return true;
                default:
                    System.out.println("操作错误，请重新输入");
                    break;
            }
            System.out.println();
            if (account == null)
                break;
        }

        System.out.println();
        return true;// 因为前面的true循环，执行不到这里了
    }

    public boolean DeleteAccount()
    {
        list.remove(list.get(idex));
        account = null;
//        delete account;
        System.out.println("销户成功!");

        return true;
    }
}